using System;
using System.Collections.Generic;
using System.Xml;
using System.Collections.ObjectModel;

namespace planning
{
	public class Jour : ObservableObject
	{
		// ----- ATTRIBUTS -----
		private NomJour nom;
		private ICollection<Enseignement> enseignements = new ObservableCollection<Enseignement>();

		// ----- CONSTRUCTEURS -----
		public Jour()
		{
			// Start of user code for default constructor
			// End of user code for default constructor
		}

		// ----- ACCESSEURS -----
		public NomJour Nom
		{
			get { return nom; }
			set { nom = value; ObjectChanged("nom"); }
		}

		public ICollection<Enseignement> Enseignements
		{
			get { return enseignements; }
			set { enseignements = value; ObjectChanged("enseignements"); }
		}


		private static Dictionary<string, Jour> references = new Dictionary<string, Jour>();

		public static Jour fromXml(XmlNode node)
		{
			Jour jour;
			XmlNode refAttribute = node.SelectSingleNode("@ref");
			XmlNode idAttribute = node.SelectSingleNode("@id");

			if(refAttribute != null) {
				if(references.ContainsKey(refAttribute.Value)) {
					return references[refAttribute.Value];
				} else {
					jour = new Jour();
					references.Add(refAttribute.Value, jour);
					return jour;
				}
			}

			if(idAttribute != null && references.ContainsKey(idAttribute.Value)) {
				jour = references[idAttribute.Value];
			} else {
				jour = new Jour();

				if(idAttribute != null) {
					references.Add(idAttribute.Value, jour);
				}
			}

			if(node.SelectSingleNode("@nom") != null)
				jour.Nom = (NomJour) Enum.Parse(typeof(NomJour), node.SelectSingleNode("@nom").Value);

			foreach (XmlNode subElement in node.SelectNodes("enseignements/*"))
			{
				Enseignement subObject = Enseignement.fromXml(subElement);
				jour.Enseignements.Add(subObject);
			}

			

			return jour;

		}

		public virtual XmlElement toXml(XmlDocument document)
		{
			// Start of user code for toXml
			// End of user code for toXml
		}


	}

}
